﻿using System;
using System.IO;
using System.Xml.Serialization;
using TaleWorlds.Core;
using TaleWorlds.Library;

namespace ExampleMod
{
	// Token: 0x0200002D RID: 45
	internal class ConfigManager
	{
		// Token: 0x170000DF RID: 223
		// (get) Token: 0x06000322 RID: 802 RVA: 0x0000E24F File Offset: 0x0000C44F
		public static ConfigManager Instance
		{
			get
			{
				if (ConfigManager._instance == null)
				{
					ConfigManager._instance = new ConfigManager();
				}
				return ConfigManager._instance;
			}
		}

		// Token: 0x06000323 RID: 803 RVA: 0x0000E26C File Offset: 0x0000C46C
		private ConfigManager()
		{
			this.configPath = Path.Combine(new string[]
			{
				BasePath.Name,
				Environment.GetFolderPath(Environment.SpecialFolder.Personal),
				"Mount and Blade II Bannerlord",
				"Configs",
				"ArmsDealer",
				"config.xml"
			});
			this._errorLogPath = Path.Combine(new string[]
			{
				BasePath.Name,
				"Modules",
				"ArmsDealer",
				"ModuleData",
				"ErrorLog.xml"
			});
		}

		// Token: 0x06000324 RID: 804 RVA: 0x0000E2FA File Offset: 0x0000C4FA
		public void WriteErrorLogEntry(string error)
		{
			Helper.WriteErrorLog(error, this._errorLogPath);
		}

		// Token: 0x06000325 RID: 805 RVA: 0x0000E308 File Offset: 0x0000C508
		public void ReadConfig()
		{
			try
			{
				Directory.CreateDirectory(Path.GetDirectoryName(this.configPath));
				if (!File.Exists(this.configPath))
				{
					this.WriteConfigWithDefaultValues();
				}
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(ArmsConfig));
				using (StreamReader streamReader = new StreamReader(this.configPath))
				{
					this.Config = (ArmsConfig)xmlSerializer.Deserialize(streamReader);
				}
				if (this.Config.ZiMuAmount == 0)
				{
					this.Config.ZiMuAmount = 3;
				}
			}
			catch (Exception)
			{
				ADInfo.Display("读取军火商配置失败，采用默认值", "");
				this.Config = new ArmsConfig();
				this.Config.ResetToDefault();
			}
		}

		// Token: 0x06000326 RID: 806 RVA: 0x0000E3D8 File Offset: 0x0000C5D8
		public void UpdateConfig()
		{
			try
			{
				Directory.CreateDirectory(Path.GetDirectoryName(this.configPath));
				this.Config.SerializeObject(this.configPath);
				this.SetAllData();
			}
			catch (Exception)
			{
			}
		}

		// Token: 0x06000327 RID: 807 RVA: 0x0000E424 File Offset: 0x0000C624
		private void SetAllData()
		{
			if (LogicArms.instance != null)
			{
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_chengying").PrimaryWeapon, "ThrustDamage", this.Config.ChengyingThrustDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_chengying").PrimaryWeapon, "ThrustSpeed", this.Config.ChengyingThrustSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_chengying").PrimaryWeapon, "SwingDamage", this.Config.ChengyingSwingDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_chengying").PrimaryWeapon, "SwingSpeed", this.Config.ChengyingSwingSpeed);
				foreach (WeaponComponentData instance in this.GetNewItem("ad_yanyue").WeaponComponent.Weapons)
				{
					LogicArms.SetNotPublicPro(instance, "SwingDamage", this.Config.ModaoSwingDamage);
					LogicArms.SetNotPublicPro(instance, "SwingSpeed", this.Config.ModaoSwingSpeed);
					LogicArms.SetNotPublicPro(instance, "ThrustDamage", this.Config.ModaoThrustDamage);
					LogicArms.SetNotPublicPro(instance, "ThrustSpeed", this.Config.ModaoThrustSpeed);
				}
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_tulong").PrimaryWeapon, "ThrustDamage", this.Config.TulongThrustDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_tulong").PrimaryWeapon, "ThrustSpeed", this.Config.TulongThrustSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_tulong").PrimaryWeapon, "SwingDamage", this.Config.TulongSwingDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_tulong").PrimaryWeapon, "SwingSpeed", this.Config.TulongSwingSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_pishan_axe").PrimaryWeapon, "SwingDamage", this.Config.PishanSwingDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_pishan_axe").PrimaryWeapon, "SwingSpeed", this.Config.PishanSwingSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia").ArmorComponent, "HeadArmor", this.Config.RuanjiaHeadArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia").ArmorComponent, "BodyArmor", this.Config.RuanjiaBodyArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia").ArmorComponent, "LegArmor", this.Config.RuanjiaLegArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia").ArmorComponent, "ArmArmor", this.Config.RuanjiaArmArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_boom"), "Weight", 0.1f * this.Config.BoomWeight);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_boom").PrimaryWeapon, "MaxDataValue", (short)this.Config.BoomStackAmount);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_boom").PrimaryWeapon, "ThrustDamage", this.Config.BoomDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_boom").PrimaryWeapon, "MissileSpeed", this.Config.BoomMissileSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("crossbow_zhuge_jian"), "Weight", 0.1f * this.Config.ZhugeWeight);
				LogicArms.SetNotPublicPro(this.GetNewItem("crossbow_zhuge_jian").PrimaryWeapon, "MaxDataValue", (short)this.Config.ZhugejianStackAmount);
				LogicArms.SetNotPublicPro(this.GetNewItem("crossbow_zhuge").PrimaryWeapon, "MaxDataValue", (short)this.Config.ZhugeStackAmount);
				LogicArms.SetNotPublicPro(this.GetNewItem("crossbow_zhuge").PrimaryWeapon, "ThrustDamage", this.Config.ZhugeThrustDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("crossbow_zhuge").PrimaryWeapon, "MissileSpeed", this.Config.ZhugeMissileSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia_new").ArmorComponent, "HeadArmor", this.Config.JinsiruanjiaHeadArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia_new").ArmorComponent, "BodyArmor", this.Config.JinsiruanjiaBodyArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia_new").ArmorComponent, "LegArmor", this.Config.JinsiruanjiaLegArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_ruanjia_new").ArmorComponent, "ArmArmor", this.Config.JinsiruanjiaArmArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_luori_arrow"), "Weight", 0.1f * this.Config.LuoriWeight);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_luori_arrow").PrimaryWeapon, "MaxDataValue", (short)this.Config.LuorijianStackAmount);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_luori_bow").PrimaryWeapon, "ThrustSpeed", (short)this.Config.LuoriThrustSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_luori_bow").PrimaryWeapon, "ThrustDamage", this.Config.LuoriThrustDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_luori_bow").PrimaryWeapon, "MissileSpeed", this.Config.LuoriMissileSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_zimu_knife").PrimaryWeapon, "MaxDataValue", this.Config.ZiMuAmount);
				foreach (WeaponComponentData instance2 in this.GetNewItem("ad_liangyin").WeaponComponent.Weapons)
				{
					LogicArms.SetNotPublicPro(instance2, "ThrustDamage", this.Config.LiangyinThrustDamage);
					LogicArms.SetNotPublicPro(instance2, "ThrustSpeed", this.Config.LiangyinThrustSpeed);
				}
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_whitehorse"), "Weight", this.Config.XuelifeiWeight);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_whitehorse").HorseComponent, "HitPointBonus", this.Config.XuelifeiHitPoints);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_whitehorse").HorseComponent, "Maneuver", this.Config.XuelifeiManeuver);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_whitehorse").HorseComponent, "ChargeDamage", this.Config.XuelifeiChargeDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_whitehorse").HorseComponent, "Speed", this.Config.XuelifeiSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_blackhorse"), "Weight", this.Config.DaoliWeight);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_blackhorse").HorseComponent, "HitPointBonus", this.Config.DaoliHitPoints);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_blackhorse").HorseComponent, "Maneuver", this.Config.DaoliManeuver);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_blackhorse").HorseComponent, "ChargeDamage", this.Config.DaoliChargeDamage);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_blackhorse").HorseComponent, "Speed", this.Config.DaoliSpeed);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_horce_armor").ArmorComponent, "BodyArmor", this.Config.HorceArmorBodyArmor);
				LogicArms.SetNotPublicPro(this.GetNewItem("ad_horce_armor_1").ArmorComponent, "BodyArmor", this.Config.HorceArmorBodyArmor1);
			}
			if (MainTroopLogic.instance != null)
			{
				bool[] female = new bool[]
				{
					this.Config.TroopGender,
					this.Config.TroopGender2,
					this.Config.TroopGender3
				};
				MainTroopLogic.instance.SetFemale(female);
			}
		}

		// Token: 0x06000328 RID: 808 RVA: 0x0000ED3C File Offset: 0x0000CF3C
		private ItemObject GetNewItem(string id)
		{
			ItemObject result = null;
			foreach (ItemObject itemObject in LogicArms.instance.weapon)
			{
				if (itemObject.StringId.Equals(id))
				{
					result = itemObject;
					break;
				}
			}
			return result;
		}

		// Token: 0x06000329 RID: 809 RVA: 0x0000EDA4 File Offset: 0x0000CFA4
		public void WriteConfigWithDefaultValues()
		{
			this.Config = new ArmsConfig();
			this.Config.ResetToDefault();
			this.UpdateConfig();
		}

		// Token: 0x0400017D RID: 381
		private static ConfigManager _instance;

		// Token: 0x0400017E RID: 382
		public ArmsConfig Config;

		// Token: 0x0400017F RID: 383
		private string configPath;

		// Token: 0x04000180 RID: 384
		private string _errorLogPath;
	}
}
